import { Controller, Get, Post, Body, Param, Delete, Query, Inject, HostParam } from '@nestjs/common';
import { BlogService } from './blog.service';
import { CreateBlogDto } from './dto/create-blog.dto';
import { UpdateBlogDto } from './dto/update-blog.dto';
import * as Minio from 'minio'
import { MINIO_CLIENT } from '../minio/minio.module';
import { RedisService } from '../redis/redis.service';
import { RequireLogin, UserInfo } from 'src/custom.decorator';
import { QueryBlogDto } from './dto/query-blog.dto';
@Controller('blog')
export class BlogController {
  constructor(private readonly blogService: BlogService) {}

  @Inject(MINIO_CLIENT)
  private minioClient: Minio.Client

  @Inject(RedisService)
  private redisService: RedisService
  @Get('test')
  async test () {
    try {
      await this.minioClient.fPutObject('photos', 'bb.json', './package.json')
      return 'http://localhost:9000/photos/bb.json'
    } catch (error) {
      console.log(error)
      return 'cuowu'
    }
  }

  @RequireLogin()
  @Post('createBlog')
  async createBlog (@Body() createBlogDto: CreateBlogDto, ) {
    return await this.blogService.createBlog(createBlogDto)
  }

  @RequireLogin()
  @Post('updateBlog')
  async updateBlog (@Body() updateBlogDto: UpdateBlogDto) {
    return await this.blogService.updateBlog(updateBlogDto)
  }

  @Get('getBlogById/:id')
  async getBlogById(@Param('id') id: number, @Query('userId') userId: number) {
    return await this.blogService.getBlogById({id, userId})
  }

  @RequireLogin()
  @Delete('delete/:id')
  async remove (@Param('id') id: number) {
    return await this.blogService.deleteBlogById(Number(id))
  }

  @Get('queryBlogList')
  async queryBlogList(@Query('page') page: number, @Query('size') size: number, @Query('status') status: number) {
    return await this.blogService.queryBlogListByPage(page, size, status)
  }

  @RequireLogin()
  @Get('queryStarBlogList')
  async queryStarBlogList(@Query('page') page: number, @Query('size') size: number, @UserInfo('userId') userId: number) {
    return await this.blogService.queryStarBlogList(page, size, userId)
  }

  @RequireLogin()
  @Get('queryLikeBlogList')
  async queryLikeBlogList(@Query('page') page: number, @Query('size') size: number, @UserInfo('userId') userId: number) {
    return await this.blogService.queryLikeBlogList(page, size, userId)
  }

  @Get('queryBlogListByParam')
  async queryMyBlogList(@Query() query: QueryBlogDto) {
    const {page, size, ...params} = query
    return await this.blogService.queryBlogListByParam(page, size, params)
  }
  @Get('getKey/:key')
  async getKey (@Param('key') key: string) {
    return await this.redisService.get(key)
  }

  @Get('read/:id')
  async read (@Param('id') id: number) {
    return await this.blogService.read(id)
  }

  @Get('getRelationArticle/:id')
  async getRelationArticle (@Param('id') id: number) {
    return await this.blogService.getRelationArticle(id)
  }

  @Get('titleExist/:title')
  async titleExist (@Param('title') title: string) {
    return await this.blogService.titleExist(title)
  }
}

